package com.zhxh.nzpos.goods;

import java.util.ArrayList;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

import com.zhxh.nzpos.BaseActivity;
import com.zhxh.nzpos.Constants;
import com.zhxh.nzpos.R;
import com.zhxh.nzpos.barcode.CaptureActivity;
import com.zhxh.nzpos.dao.GoodsDao;
import com.zhxh.nzpos.model.Goods;
import com.zhxh.nzpos.sell.ItemDeleteCallback;

/**
 * 商品管理
 * 
 * @author wangzt
 *
 */
public class GoodsManageActivity extends BaseActivity implements ItemDeleteCallback {

	private ArrayList<GoodsItem> goodsItems;
	private GoodsListAdapter listAdapter;
	GoodsDao goodsDao;
	private int position;
	
	 EditText dealOcr;
	
	
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_goods_manage);
        goodsDao = new GoodsDao(this);
        initViews();
    }
	
	private void initViews() {
		ListView listView = (ListView) findViewById(R.id.goods_listview);
		//goodsItems = new ArrayList<GoodsItem>();
		initData();
		// TODO 扫描条码添加一项
		dealOcr = (EditText) findViewById(R.id.goods_ocr_et);
		ImageView addBtn = (ImageView) findViewById(R.id.goods_add_btn);
		addBtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Editable ocr = dealOcr.getText();
				if (ocr!= null) {
					String ocrStr = ocr.toString();
					// TODO 需要从数据库中根据编号查找
					Intent intent = new Intent();
					intent.setClass(getApplicationContext(), GoodsAddActivity.class);
					//startActivity(intent);
					startActivityForResult(intent, 0);
				}
			}
		});
		
		
		Button scanBtn = (Button) findViewById(R.id.buyin_scan_btn);
		scanBtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				//Intent intent = new Intent(Constants.SCAN_ACTION);
				Intent intent = new Intent();
				intent.setClass(getApplicationContext(), CaptureActivity.class);
				startActivityForResult(intent, Constants.SCAN_REQUEST_CODE);
			}
		});
		
		
		listAdapter = new GoodsListAdapter(getLayoutInflater(), goodsItems, this);
		listView.setAdapter(listAdapter);
		
/*		ImageView cancelBtn = (ImageView) findViewById(R.id.goods_cancel_btn);
		cancelBtn.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				finish();
			}
		});*/

		
	}

	public void onDelete(int pos) {

		position = pos;
		new AlertDialog.Builder(this)
				.setTitle(R.string.goods_category_delete_dialog_title)
				.setMessage(R.string.goods_delete_dialog_content)
				.setPositiveButton("确定", new DialogInterface.OnClickListener() {

					public void onClick(DialogInterface dialog, int which) {

						GoodsItem goods  = goodsItems.get(position);
						goodsDao.delete(goods.getId());
						goodsItems.remove(position);
						listAdapter.notifyDataSetChanged();
						// 确定按钮事件
						// setResult(RESULT_OK);
						dialog.dismiss();
						// finish();
					}
				})
				.setNegativeButton("取消", new DialogInterface.OnClickListener() {

					public void onClick(DialogInterface dialog, int which) {
						// 取消按钮事件
						dialog.dismiss();
					}
				}).show();
	}
	
	private void initData() {
		goodsItems = new ArrayList<GoodsItem>();
	//	DBHelper dbHelper = new DBHelper(getApplicationContext());
		//dbHelper.openDatabase();	
		ArrayList<Goods>  goods = (ArrayList)goodsDao.getAll();
		for(Goods goodsz : goods) {
			GoodsItem item = copyProperties (goodsz);
			goodsItems.add(item);
		}
		 
		
	}
	
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		
		switch (resultCode){  
        case RESULT_OK:  
        	if (requestCode == Constants.SCAN_REQUEST_CODE) {
    			if (resultCode == RESULT_OK) {
    				String ocrStr = data.getStringExtra(Constants.SCAN_RESULT_EXTRA);
    				if (!TextUtils.isEmpty(ocrStr)) {
    					dealOcr.setText(ocrStr);
    				} else {
    					Toast.makeText(getApplicationContext(), "请重新扫描！",
    							Toast.LENGTH_LONG).show();
    				}

    			} else if (resultCode == RESULT_CANCELED) {
    				Toast.makeText(getApplicationContext(), "请重新扫描！",
    						Toast.LENGTH_LONG).show();
    			}
    	
    			}
    		else {
		          Bundle b = data.getExtras();  
		          GoodsItem goods =  (GoodsItem)b.getSerializable("goods");
		         // String string = b.getString("CALCULATION");  
		          goodsItems.add(0,goods);
					if (listAdapter != null) {
						listAdapter.notifyDataSetChanged();
					}  
				}
           
        }  

	}
	

	public void search(View view) {
		final EditText dealOcr = (EditText) findViewById(R.id.goods_ocr_et);
		Editable ocr = dealOcr.getText();
		if (ocr!= null) {
			String ocrStr = ocr.toString();
			if(goodsItems == null) {
				goodsItems = new ArrayList<GoodsItem>();
			}
			else {
				goodsItems.clear();
				
			}
			GoodsDao dao = new GoodsDao(this);
			Goods goods = dao.find(ocrStr.trim());
			if(goods != null) {
				GoodsItem item = copyProperties (goods);
		        goodsItems.add(item);
					if (listAdapter != null) {
						listAdapter.notifyDataSetChanged();
					}
			}
		}
		
	}
	
	private GoodsItem copyProperties (Goods goods) {
		GoodsItem item = new GoodsItem();
		item.setId(goods.getId());
		item.setCategoryId(goods.getCategoryId());
		item.setCategoryName(goods.getCategoryName());
		item.setName(goods.getName());
		item.setNote(goods.getNote());
		item.setPrice1(goods.getPrice1());
		item.setPrice2(goods.getPrice2());
		item.setSpecification(goods.getSpecification());
		item.setUnits1(goods.getUnits1());
		item.setUnits2(goods.getUnits2());
		item.setCode(goods.getCode());
		item.setPoints(goods.getPonits());
		return item;
	}

	@Override
	public void onUpdate() {
		// TODO Auto-generated method stub
		
	}
	
}
